//
//  CarRadarDataCell.m
//  New_Runway
//
//  Created by mack on 2018/2/23.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarRadarDataCell.h"


@interface CarRadarDataCell ()
@property (nonatomic, strong)  PYEchartsView *kEchartView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSArray *array1;
@end

@implementation CarRadarDataCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self creatTitle];
        [self addAllViews];
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
-(void)creatTitle{
    kSelfWeak;
    UILabel *titleL = [[UILabel alloc] init];
    [self addSubview:titleL];
    titleL.textColor = MAIN_FONT_COLOR();
    titleL.textAlignment = NSTextAlignmentLeft;
    titleL.text = @"注： 橘色面积越大性价比越高，越值得购买";
    titleL.font = [UIFont systemFontOfSize:12];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf);
        make.left.equalTo(weakSelf).mas_offset(20);
        make.bottom.equalTo(weakSelf).mas_offset(-13);
    }];
    
}

-(void)addAllViews{
    
    _kEchartView = [[PYEchartsView alloc] init];
    [self addSubview:_kEchartView];
    kSelfWeak;
    [_kEchartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).mas_offset(-10);
        make.left.right.bottom.equalTo(weakSelf);
    }];
    [self initRagarView];
    
    
}

-(void)initRagarView{
    self.dataArray = [[NSMutableArray alloc] init];
//    {\"title\":{\"text\":\"\",\"subtext\":\"\"},\"tooltip\":{\"\":\"\"},\"legend\":{\"x\":\"center\",\"data\":[\"\",\"\"]},\"toolbox\":{\"show\":false,\"feature\":{\"mark\":{\"show\":false},\"dataView\":{\"show\":false,\"readOnly\":false}}}}
    NSString *json = @"";
    NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
    PYOption *option = [RMMapper objectWithClass:[PYOption class] fromDictionary:jsonDic];
    option.animation = YES;
    PYPolar *polar = [[PYPolar alloc] init];
    polar.type = PYPolarTypePolygon;
    polar.radius = @"70%";
    _array1 =  @[@{
                             @"text":@"排量",
                             @"max":@6

                             },
                         @{
                             @"text":@"车龄",
                             @"max":@15

                             },
                         @{
                             @"text":@"里程",
                             @"max":@20

                             },
                         @{
                             @"text":@"价格",
                             @"max":@80
                             
                             },
                         @{
                             @"text":@"过户",
                             @"max":@10

                             
                             }];
    polar.radius = @100;
    NSMutableArray *polars =[[NSMutableArray alloc]init];
    [polars addObjectsFromArray:_array1];
    polar.indicator = polars;
    
    NSMutableArray *ploarArray = [[NSMutableArray alloc] init];
    [ploarArray addObject:polar];
    [option setPolar:ploarArray];
    
    PYSeries *series = [[PYSeries alloc] init];
    series.type = PYSeriesTypeRadar;
    series.name = @"偏爱车辆";
    series.tooltip = [[PYTooltip alloc] init];
    series.clickable = YES;
    series.itemStyle = [[PYItemStyle alloc] init];
    series.itemStyle = [[PYItemStyle alloc] init];
    series.itemStyle.normal = [[PYItemStyleProp alloc] init];
    series.itemStyle.normal.lineStyle = [[PYLineStyle alloc] init];
    series.itemStyle.normal.areaStyle = [[PYAreaStyle alloc] init];
    series.itemStyle.normal.areaStyle.type = PYAreaStyleTypeDefault ;
    series.itemStyle.normal.lineStyle.shadowColor = PYRGBA(0, 0, 0, .4);
    NSMutableArray *valueArr = [[NSMutableArray alloc] init];
    NSDictionary *dic =
    @{
      @"name":@"偏爱系数",
      @"value":self.dataArray
      };
    
    [valueArr addObject:dic];
    series.data = valueArr;
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    [dataArray addObject:series];
    [option setSeries:dataArray];
    
    [_kEchartView setOption:option];
    
    
}

- (void)setCarInfo:(CarResult *)carInfo{

    _carInfo = carInfo;
    [_dataArray removeAllObjects];
    if (carInfo.carRadarInfo.carDisplacement > 6) {
        [_dataArray addObject:@(6)];

    }else{
        [_dataArray addObject:@(carInfo.carRadarInfo.carDisplacement)];

    }
    
    if ( 15 > carInfo.carRadarInfo.carAge > 0) {
         [_dataArray addObject:@(15 - carInfo.carRadarInfo.carAge)];
    }else if(carInfo.carRadarInfo.carAge > 15){
         [_dataArray addObject:@(0)];
    }else{
      
        [_dataArray addObject:@(15)];

    }
    
    if (20 > carInfo.carRadarInfo.carMileage >0) {
        [_dataArray addObject:@(20 - carInfo.carRadarInfo.carMileage)];

    }else if(carInfo.carRadarInfo.carMileage > 20){

        [_dataArray addObject:@(0)];
    }else{
        [_dataArray addObject:@(20)];
    }
    
    if (carInfo.carRadarInfo.carRetailPrice > 80) {
        [_dataArray addObject:@(0)];
    }else if(80 > carInfo.carRadarInfo.carRetailPrice > 0){
    
        [_dataArray addObject: @(80 - carInfo.carRadarInfo.carRetailPrice)];
    }else{
        [_dataArray addObject:@(80)];
    }
    if (10 > carInfo.carRadarInfo.carTransferNum > 0) {
       [_dataArray addObject:@(10 - carInfo.carRadarInfo.carTransferNum)];
    }else if(carInfo.carRadarInfo.carTransferNum <= 0){
        [_dataArray addObject:@(10)];
    }else{
        [_dataArray addObject:@(0)];

    }

    [_kEchartView loadEcharts];

}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

//-(NSInteger)differYear:(NSInteger )fromDate{
//
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateStyle:NSDateFormatterMediumStyle];
//    [formatter setTimeStyle:NSDateFormatterShortStyle];
//    [formatter setDateFormat:@"YYYY-MM-dd"];
//    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
//    [formatter setTimeZone:timeZone];
//    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:fromDate / 1000.0];
//    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//    [gregorian setFirstWeekday:2];
//    NSDate *toDate = [NSDate date];
//    NSDateComponents *components = [gregorian components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:confromTimesp toDate:toDate options:0];
//
//    return components.year;
//}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
